<?php
namespace Home\Controller;
use Home\Model\ManagerModel;
use Home\Model\ClassModel;
use Home\Model\CateModel;
use Home\Model\OrderModel;
class IndexController extends BaseController {
    public function index(){
       $this->display("Manage/login");
    }
    public function Login(){
        $manageModel=new ManagerModel();
        $username=I("username");
        $password=I("password");
        $where=array('username'=>$username,'password'=>$password);
        $result=$manageModel->getManageItem($where);
        $uid=$result['id'];
        if($result){
            cookie('uid',$uid,60*60*24*7);
            $this->redirect("Manager/categoryList");
        }else{
          $this->redirect("Index/Login");
        }
    }
    public function clearUser(){
        cookie('uid',null);
        $this->redirect("Index/index");
    }
    
    public function courseIn(){
        $callback  = urlencode(strtolower(C('DOMAIN').U('Sign/add'))); //回调地址
        $state  ='courseList'; //E职课
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->_APPID.'&redirect_uri='.$callback.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
        header("Location:".$url);
    }
    public function mycourseIn(){
        $callback  = urlencode(strtolower(C('DOMAIN').U('sign/add'))); //回调地址
        $state  ='mycourse'; //E职课
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->_APPID.'&redirect_uri='.$callback.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
        header("Location:".$url);
    }
    public function courseList(){
        //默认显示所有的
        $cateid=intval(I("cateid"));
        if(!empty($cateid)){
            $where['cateid']=$cateid;
        }
        $classModel=new ClassModel();
        $calssList=$classModel->getclassList($where);
        $cateModel=new CateModel();
        $cateList=$cateModel->getCateList();
        $this->assign("cateList",$cateList);
        $this->assign("classList",$calssList);
        $this->display("Index/index");
    }
    public function couresDetail(){
        $id=intval(I("id"));
        $classModel=new ClassModel();
        $where['c.id']=$id;
        $classInfo=$classModel->getclassList($where);
        $classInfo=$classInfo[0];
        $this->assign("classInfo",$classInfo);
        $this->display("Index/detail");
    }
    public function listen(){
        //判断订单表是否存在
        $orderModel=new OrderModel();
        $classid=intval(I("classid"));
        $uid=intval($this->_userid);
        if($uid==0){
            $this->redirect("Index/courseIn");exit();
        }
        $where['class_id']=$classid;
        $where['uid']=$uid;
        $where['status']=1;
        $result=$orderModel->getorderItem($where);
        if($result){//已经支付过，直接收听
            $this->assign("classid",$classid);
            $this->redirect("View/index");
            exit();
        }else{
            //查询这个课程是否免费，如果免费，添加订单表，直接收听
            $classModel=new ClassModel();
            $orderModel=new OrderModel();
            $calss_where['id']=$classid;
            $classInfo=$classModel->getClassItem($calss_where);
            $money=$classInfo['nowprice'];
            if($money>0){
                //跳转支付
                $data['class_id']=$classid;
                $data['uid']=$uid;
                $data['status']=2;
                $data['money']=$money;
                $data['ordersn']="1358850802".date("YmdHis");
                $orderid=$orderModel->addorder($data);
                //调用支付
                $this->redirect("Payment/wechatpay",array('orderid'=>$orderid));
                exit();
            }else{
                //免费，添加订单表
                $data['class_id']=$classid;
                $data['uid']=$uid;
                $data['status']=1;
                $data['money']=0;
                $data['ordersn']="1323222001".date("YmdHis");
                $data['transid']='free';
                $orderid=$orderModel->addorder($data);
            }
            $this->redirect("View/index");
        }
      
    }
    public function mycourse(){
          $uid=intval($this->_userid);
        //查询订单  
        $sql="SELECT c.*,u.nickname,headimgurl  FROM course_order o,course_class c,course_user u
              WHERE o.class_id=c.id AND c.uid=u.id and o.status=1 AND o.uid=".$uid;
        $calssList=M()->query($sql);
        $this->assign("classList",$calssList);
        $this->display("Index/myclass");
    }
}